//
//  EMGeeRTViewController.m
//  PictureSelector
//
//  Created by Lyy on 15/8/24.
//  Copyright (c) 2015年 Lyy. All rights reserved.
//

// 直播

#import "EMGeeRTViewController.h"
#import <AVFoundation/AVFoundation.h>
#if !TARGET_IPHONE_SIMULATOR
#import <RtSDK/RtSDK.h>
#endif

@interface EMGeeRTViewController ()
#if !TARGET_IPHONE_SIMULATOR
<GSBroadcastDelegate,
GSBroadcastAudioDelegate,
GSBroadcastVideoDelegate
>
#endif

#if !TARGET_IPHONE_SIMULATOR
@property (nonatomic ,strong)GSBroadcastManager *broadcastManager;
@property (strong, nonatomic)GSVideoView *gsvideoView;
#endif

@end


@implementation EMGeeRTViewController

- (void)viewDidLoad {
    [super viewDidLoad];
#if !TARGET_IPHONE_SIMULATOR
    [self createVideoView];
    [self configBroadcastParams];
#endif
    
}

#if !TARGET_IPHONE_SIMULATOR
- (void)configBroadcastParams {
    
//    NSString *uuid = [EMUUID getEMUUID];
//    uuid = [NSString stringWithFormat:@"%@_%d_%@",uuid,KPlatformID,[[EMAPP app] versionDescription]];
    
    _broadcastManager = [GSBroadcastManager sharedBroadcastManager];
    _broadcastManager.broadcastDelegate = self;
    _broadcastManager.audioDelegate = self;
    _broadcastManager.videoDelegate = self;
    
    GSConnectInfo *connectInfo = [GSConnectInfo new];
    connectInfo.domain = @"emoney.gensee.com";
    connectInfo.serviceType = GSBroadcastServiceTypeWebcast;
    connectInfo.roomNumber = @"94324272";
    connectInfo.nickName = @"fsdfsdf";  //uuid;
    connectInfo.watchPassword = @"888888";
    [_broadcastManager connectBroadcastWithConnectInfo:connectInfo];
}

- (void)createVideoView {
    self.gsvideoView = [[GSVideoView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:self.gsvideoView];
    [self.view insertSubview:self.gsvideoView belowSubview:self.videoView];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.gsvideoView.frame = self.videoView.frame;
}

#pragma mark - GSBroadcastDelegate

// 直播初始化代理
- (void)broadcastManager:(GSBroadcastManager*)manager didReceiveBroadcastConnectResult:(GSBroadcastConnectResult)result
{
    switch (result) {
        case GSBroadcastConnectResultSuccess:
            // 直播初始化成功，加入直播
            if (![self.broadcastManager join]) {
                
            }
            
            break;
            
        case GSBroadcastConnectResultInitFailed:
            
        case GSBroadcastConnectResultJoinCastPasswordError:
            
        case GSBroadcastConnectResultWebcastIDInvalid:
            
        case GSBroadcastConnectResultRoleOrDomainError:
            
        case GSBroadcastConnectResultLoginFailed:
            
        case GSBroadcastConnectResultNetworkError:
            
        case GSBroadcastConnectResultWebcastIDNotFound:
        {
            //            [self.progressHUD hide:YES];
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:  NSLocalizedString(@"BroadcastConnectionError",  @"直播连接失败提示") delegate:nil cancelButtonTitle:NSLocalizedString(@"OK",  @"确认") otherButtonTitles:nil, nil];
            [alertView show];
        }
            break;
            
            
        default:
            break;
    }
}

#pragma mark -
#pragma mark GSBroadcastAudioDelegate
/**
 *  音频模块初始化反馈代理
 *
 *  @param manager 触发此代理的GSBroadcastManager对象
 *  @param result  布尔值表示音频模块是否加载成功，YES表示成功
 *  @see GSBroadcastManager
 */
- (void)broadcastManager:(GSBroadcastManager*)manager didReceiveAudioModuleInitResult:(BOOL)result{
    
    if (result) {
        //        打开扩音器
        [_broadcastManager activateSpeaker];
        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        [audioSession setActive:YES error:nil];
        [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord
                      withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker
                            error:nil];
        
    }
    
}
#pragma mark -
#pragma mark GSBroadcastVideoDelegate

- (void)broadcastManager:(GSBroadcastManager*)manager userID:(long long)userID renderVideoFrame:(GSVideoFrame*)videoFrame {
    // 指定Videoview渲染每一帧数据
    [_gsvideoView renderVideoFrame:videoFrame];
}

// 收到一路视频
- (void)broadcastManager:(GSBroadcastManager*)manager didUserJoinVideo:(GSUserInfo *)userInfo
{
    
}

// 某一路摄像头视频被激活
- (void)broadcastManager:(GSBroadcastManager*)manager didSetVideo:(GSUserInfo*)userInfo active:(BOOL)active
{
    [self.broadcastManager undisplayVideo:userInfo.userID];
    
    [self.broadcastManager displayVideo:userInfo.userID];
}

// 某一路视频播放代理
- (void)broadcastManager:(GSBroadcastManager*)manager didDisplayVideo:(GSUserInfo*)userInfo
{
    
}

// 某一路视频关闭播放代理
- (void)broadcastManager:(GSBroadcastManager*)manager didUndisplayVideo:(long long)userID
{
    
    
}

- (void)dealloc
{
    // 退出直播，但不结束直播, 若参数为YES,直播将同时结束
    [self.broadcastManager leaveAndShouldTerminateBroadcast:NO];
    // 释放资源
    [self.broadcastManager invalidate];
    
}
#endif

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
